#!/usr/bin/env bash

# Source the script 'common-functions.sh'.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR")
source "$ROOT_DIR/common-functions.sh"

_main() {
    local input_files=""
    local output_dir=""

    # Execute initial checks.
    _check_dependencies "
        command=convert; pkg_manager=apt; package=imagemagick |
        command=convert; pkg_manager=dnf; package=ImageMagick |
        command=convert; pkg_manager=pacman; package=imagemagick |
        command=convert; pkg_manager=zypper; package=ImageMagick"
    _display_wait_box "2"
    input_files=$(_get_files "par_type=file; par_select_mime=image/")
    output_dir=$(_get_output_dir "par_use_same_dir=false")

    # Execute the function '_main_task' for each file in parallel.
    _run_task_parallel "$input_files" "$output_dir"
    _display_result_box "$output_dir"
}

_main_task() {
    local input_file=$1
    local output_dir=$2
    local output_file=""
    local std_output=""
    local temp_file1=""
    local temp_file2=""
    local temp_file3=""
    local temp_file4=""
    local icon_size="256"

    # Work on a temporary file.
    temp_file1="$(_get_temp_file_dry).png"
    temp_file2="$(_get_temp_file_dry).png"
    temp_file3="$(_get_temp_file_dry).png"
    temp_file4="$(_get_temp_file_dry).png"

    # Run the process (part 0) 'export svg'.
    local filename_extension=""
    local input_file_proc=""
    filename_extension=$(_get_filename_extension "$input_file")
    if [[ "${filename_extension,,}" == ".svg"* ]]; then

        local inkscape_version=""
        local inkscape_version_old="1.0"
        inkscape_version=$(inkscape --version | cut -d " " -f 2)
        if [[ "$inkscape_version" == $(echo -e "$inkscape_version\n$inkscape_version_old" | sort -V | head -n1) ]]; then
            # Old Inkscape version.
            std_output=$(inkscape --without-gui --export-area-drawing --file="$input_file" --export-height=$((icon_size * 2)) --export-png="$temp_file4" 2>&1)
        else
            # New Inkscape version.
            std_output=$(inkscape --export-area-drawing --export-height=$((icon_size * 2)) --export-filename="$temp_file4" -- "$input_file" 2>&1)
        fi
        _check_output "$?" "$std_output" "$input_file" "$temp_file4" || return 1

        input_file_proc=$temp_file4
    else
        input_file_proc=$input_file
    fi

    # Run the process (part 1) 'get information of the image'.
    local first_pixel_color=""
    first_pixel_color=$(convert "$input_file_proc" -format "%[pixel:p{0,0}]" "info:" 2>&1)
    _check_output "$?" "$std_output" "$input_file" "" || return 1

    local last_pixel=""
    last_pixel=$(identify -format "%[fx:w-1],0" "$input_file_proc" 2>&1)
    _check_output "$?" "$std_output" "$input_file" "" || return 1

    local last_pixel_color=""
    last_pixel_color=$(convert "$input_file_proc" -format "%[pixel:p{$last_pixel}]" "info:")
    _check_output "$?" "$std_output" "$input_file" "" || return 1

    if [[ "$first_pixel_color" == "$last_pixel_color" ]]; then
        # Run the process (part 2) 'automatic crop the content'.
        std_output=$(convert "$input_file_proc" -trim +repage "$temp_file1" 2>&1)
        _check_output "$?" "$std_output" "$input_file" "$temp_file1" || return 1

        # Run the process (part 3) 'extend to square'.
        std_output=$(convert "$temp_file1" -set option:distort:viewport "%[fx:max(w,h)]x%[fx:max(w,h)]+%[fx:(w-max(w,h))/2]+%[fx:(h-max(w,h))/2]" -virtual-pixel transparent -filter point -distort SRT 0 -bordercolor "$first_pixel_color" -border 1 -fill "$first_pixel_color" -draw "color 0,0 floodfill" -shave 1 -draw "color 0,0 floodfill" +repage "$temp_file2" 2>&1)
        _check_output "$?" "$std_output" "$input_file" "$temp_file2" || return 1

        # Run the process (part 4) 'extend 14%' to add a border.
        local factor="1.14"
        std_output=$(convert "$temp_file2" -set option:distort:viewport "%[fx:(w*$factor)]x%[fx:(h*$factor)]+%[fx:(w-(w*$factor))/2]+%[fx:(h-(h*$factor))/2]" -virtual-pixel transparent -filter point -distort SRT 0 -bordercolor "$first_pixel_color" -border 1 -fill "$first_pixel_color" -draw "color 0,0 floodfill" -shave 1 -draw "color 0,0 floodfill" +repage "$temp_file3" 2>&1)
        _check_output "$?" "$std_output" "$input_file" "$temp_file3" || return 1

        input_file_proc=$temp_file3
    fi

    # Run the process (part 5) 'resize and crop the final image' to add a border.
    std_output=$(convert "$input_file_proc" -resize "${icon_size}x${icon_size}^" -gravity center -crop ${icon_size}x${icon_size}+0+0 -strip +repage "$temp_file4" 2>&1)
    _check_output "$?" "$std_output" "$input_file" "$temp_file4" || return 1

    # Move the temporary file to the output.
    output_file=$(_get_output_filename "$input_file" "$output_dir" "par_extension_opt=replace; par_extension=png")
    _move_temp_file_to_output "$input_file" "$temp_file4" "$output_file"

    # Remove the temporary files on each iteration (if not removed before).
    rm -f -- "$temp_file1"
    rm -f -- "$temp_file2"
    rm -f -- "$temp_file3"
    rm -f -- "$temp_file4"
}

_main "$@"
